package dletc.job;
/**
使用生产者和消费者模式实现，交替输出：
	假设只有两个线程，输出以下结果：
		t1-->1
		t2-->2
		t1-->3
		t2-->4
		t1-->5
		t2-->6
 */
public class Job {
	
	public static void main(String[] args) {
		
		
		
		Num num = new Num();
		
		Thread t1 = new Thread(num);
		Thread t2 = new Thread(num);
		
		t1.setName("t1");
		t2.setName("t2");
		
		t1.start();
		t2.start();
	}	

}

class Num implements Runnable{
	
	 Integer i = 0;

	@Override
	public void run() {
		
		while(true){
			
			synchronized (this) {
				
				if( (i % 2) == 0 ){
					try {
						this.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					System.out.println(Thread.currentThread().getName() + "------>" + i++);
					this.notifyAll();
				} 
					
			}
			
			synchronized (this) {
				
				if( (i % 2) != 0 ){
					try {
						i.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					System.out.println(Thread.currentThread().getName() + "------>" + i++);
					this.notifyAll();
				} 
					
			}
			
			
		}
		
	
		
		
	}
	
}


